<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>白名单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="table-search">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">选择区服:</label>
                            <div class="layui-input-inline">
                                <select name="gameArea" class="layui-select">
                                    <option v-for="(item,index) in services" :value="item.server_id" :selected="index==0">{{item.name}}</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">长ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="customerId" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">记录时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="createDateBegin" id="createTime0"
                                       placeholder="开始时间" autocomplete="off" class="layui-input" />
                                <input type="text" name="createDateEnd" id="createTime1"
                                       placeholder="截止时间" autocomplete="off" class="layui-input" />
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>

        </fieldset>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add" auth="banList:add">批量添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="editlist" >批量修改</button>
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="deletelist" >批量删除</button>
    </div>
</script>
<table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" auth="banList:edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete" auth="banList:delete">删除</a>
</script>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script>
    let services = JSON.parse(localStorage.getItem('services'));
    services.unshift({ server_id: 0, name: '全部' });
    new Vue({
        el: "#app",
        data: {
            services:services,
        },
        mounted:function () {
            layui.use(['form', 'table','laydate'], function () {
                var isLodedTable = false;
                var $ = layui.jquery,
                    form = layui.form,
                    table = layui.table
                    ,laydate=layui.laydate;

                laydate.render({
                    elem: '#createTime0',
                    value: getYmd(0)
                });
                laydate.render({
                    elem: '#createTime1',
                    value: getYmd(1)
                });
                var loadTable = function(){
                    table.render({
                        where: form.val('table-search'),
                        elem: '#currentTableId',
                        url: '/admin/banList/paginate',
                        defaultToolbar: ['filter', 'print' ],
                        toolbar: '#toolbarDemo',
                        cols: [[
                            {type: "checkbox", width: 50},
                            {title: '长ID',field:"customer_id", width: 200},
                            {title: '角色信息',field:"roles", width: 200},
                            {title: '拉黑原因', width: 200,  field:"cause"},
                            {title: '创建时间', width: 200,  field:"created_at"},
                            {title: '解封时间', width: 200,  field:"enable_at"},
                            {title: '黑名单状态', width: 200, field:"status"},
                           // {title: '设备号', width: 200, field:"deviceCode"},
                            {title: '操作', minWidth: 300, toolbar: '#currentTableBar', align: "center"}
                        ]],
                        limits: [10, 15, 20, 25, 50, 100],
                        limit: 15,
                        page: true,
                        skin: 'line'
                    });
                    isLodedTable = true;
                }
                loadTable();

                // 监听搜索操作
                form.on('submit(data-search-btn)', function (data) {
                    if (!isLodedTable){
                        loadTable();
                    } else {
                        //执行搜索重载
                        table.reload('currentTableId', {
                            page: {
                                curr: 1
                            }
                            , where: form.val('table-search')
                        }, 'data');
                    }
                    return false;
                });

                /**
                 * toolbar监听事件
                 */
                table.on('toolbar(currentTableFilter)', function (obj) {
                    var selectedData = table.checkStatus(obj.config.id).data;
                    var Ids = selectedData.map(function(data) {
                        return data.customer_id;
                    }).join(',');
                    var cids = selectedData.map(function(data) {
                        return data.id;
                    }).join(',');
                    console.log('选中的数据',Ids)
                    if(obj.event === 'add'){ //添加禁言
                        layer.open({
                            type: 2, // page 层类型
                            area: ['780px', '500px'],
                            title: '添加',
                            shade: 0.6, // 遮罩透明度
                            shadeClose: true,
                            maxmin: true, // 允许全屏最小化
                            content: "add.html"
                        });
                    }

                    if(obj.event === 'editlist'){ //批量编辑
                        layer.open({
                            type: 2, // page 层类型
                            area: ['780px', '500px'],
                            title: '添加',
                            shade: 0.6, // 遮罩透明度
                            shadeClose: true,
                            maxmin: true, // 允许全屏最小化
                            content: "editlist.html?Ids=" + Ids
                        });
                    }


                    if(obj.event === 'deletelist'){ //批量删除
                            layer.confirm('您确定删除吗?',{title:"提示"},function(){
                                $.post('/admin/banList/'+cids,{_method:'delete'},function(res){
                                    if (res.code === 0) {
                                        layer.msg('success', {icon: 6, time: 500}, function () {
                                            location.reload();//父页面刷新
                                        })
                                    } else {
                                        layer.msg(res.msg, {icon: 5})
                                    }
                                })
                            })

                    }
                });

                table.on('tool(currentTableFilter)', function (obj) {
                    var data = obj.data;
                    if (obj.event==='delete'){
                        layer.confirm('您确定删除吗?',{title:"提示"},function(){
                            $.post('/admin/banList/'+data.id,{_method:'delete'},function(res){
                                if (res.code === 0) {
                                    layer.msg('success', {icon: 6, time: 500}, function () {
                                        location.reload();//父页面刷新
                                    })
                                } else {
                                    layer.msg(res.msg, {icon: 5})
                                }
                            })
                        })
                        } else if (obj.event === 'edit') {
                        layer.open({
                            type: 2, // page 层类型
                            area: ['780px', '500px'],
                            title: '编辑',
                            shade: 0.6, // 遮罩透明度
                            shadeClose: true,
                            maxmin: true, // 允许全屏最小化
                            content: "edit.html?id=" + obj.data.id
                        });
                    }
                });

                form.render()
            });
        }
    })

</script>
</body>
</html>